package com.tiocloud.chat.feature.main.fragment;

import android.view.View;

import com.tiocloud.chat.R;
import com.tiocloud.chat.feature.home.chat.ChatFragment;
import com.tiocloud.chat.feature.main.base.MainTabFragment;
import com.tiocloud.chat.feature.search.curr.SearchActivity;
import com.tiocloud.chat.widget.titlebar.HomeTitleBar;
import com.watayouxiang.imclient.TioIMClient;
import com.watayouxiang.imclient.client.IMState;
import com.watayouxiang.imclient.event.TioStateEvent;
import com.watayouxiang.androidutils.page.TioActivity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * author : TaoWang
 * date : 2020-01-10
 * desc : 会话
 * <p>
 * tabData: {@link com.tiocloud.chat.feature.main.model.MainTab#CHAT}
 */
public class MainChatFragment extends MainTabFragment {

    private HomeTitleBar homeTitleBar;
    private ChatFragment fragment;

    @Override
    public boolean isRegisterEvent() {
        return true;
    }

    @Override
    protected void onInit() {
        setStatusBarCustom(findViewById(R.id.fl_statusBar));

        homeTitleBar = findViewById(R.id.homeTitleBar);
        homeTitleBar.setTitle("聊天");

        IMState imState = TioIMClient.getInstance().getState();
        boolean isConnect = imState == IMState.CONNECTED;
        setAppendTitle(isConnect ? null : "未连接");

        fragment = new ChatFragment();
        fragment.setContainerId(R.id.chat_fragment_container);
        TioActivity tioActivity = (TioActivity) getActivity();
        if (tioActivity != null) {
            tioActivity.replaceFragment(fragment);
        }

        findViewById(R.id.ll_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchActivity.start(v.getContext());
            }
        });
    }

    // ====================================================================================
    // 通知 fragment 刷新数据
    // ====================================================================================

    @Override
    public void onPageShow(int count, boolean isInit) {
        super.onPageShow(count, isInit);
        setStatusBarLightMode(true);
    }

    @Override
    protected void onRefresh() {
        super.onRefresh();

    }

    @Override
    public void onTabDoubleTap() {
        super.onTabDoubleTap();

    }

    // ====================================================================================
    // 长连接状态
    // ====================================================================================

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTioStateEvent(TioStateEvent event) {
        switch (event.state) {
            case CONNECTING:
                setAppendTitle("连接中...");
                break;
            case CONNECT:
                setAppendTitle(null);
                break;
            case ERROR:
            case DISCONNECT:
                setAppendTitle("未连接");
                break;
        }
    }

    private void setAppendTitle(String append) {
        if (homeTitleBar != null) {
            homeTitleBar.setAppendTitle(append);
        }
    }
}
